package com.bhs.sansong.ui.order;

import static com.google.android.material.tabs.TabLayout.MODE_FIXED;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.FragmentAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.EnquiryOrderRefresh;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.view.SelectedFont;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 询价订单
 */
public class EnquiryOrderActivity extends BaseActivity {

    private LinearLayout mLlSearch;
    private EditText mEtSearch;
    private TextView mTvScreen;
    private TabLayout mTabLayout;
    private ViewPager2 mViewPage;
    private List<TabBean> tabBeanList = new ArrayList<>();
    @Override
    public int initLayout() {
        return R.layout.activity_enquiry_order;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("询价列表");
        mLlSearch = findViewById(R.id.ll_search);
        mEtSearch = findViewById(R.id.et_search);
        mTvScreen = findViewById(R.id.tv_screen);
        mTabLayout = findViewById(R.id.tabLayout);
        mViewPage = findViewById(R.id.view_page);
    }

    @Override
    public void initData() {
        tabBeanList.add(new TabBean("询价中", "1"));
        tabBeanList.add(new TabBean("成交单", "2"));
        tabBeanList.add(new TabBean("取消单", "3"));
        mTabLayout.setTabMode(MODE_FIXED);
        //tab切换时候的字体设置
        mTabLayout.addOnTabSelectedListener(new SelectedFont(this));
        ArrayList<BaseFragment> fragments = new ArrayList<>();

        fragments.add(EnquiryOrderListFragment.newInstance(tabBeanList.get(0).getType()));
        fragments.add(EnquiryOrderListFragment.newInstance(tabBeanList.get(1).getType()));
        fragments.add(EnquiryOrderListFragment.newInstance(tabBeanList.get(2).getType()));
        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(this, fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabBeanList.get(position).getTypeName());
            }
        }).attach();

        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    EventBus.getDefault().post(new EnquiryOrderRefresh(mEtSearch.getText().toString()));
                    return true;
                }
                return false;
            }
        });
    }
}